﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Common;
using Modules.Project.Repositories;
using PmSoft;

namespace Modules.Project
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectSecurityDepositService
    {
        private readonly ProjectSecurityDepositRepository projectSecurityDepositRepository;

        public ProjectSecurityDepositService(ProjectSecurityDepositRepository projectSecurityDepositRepository)
        {
            this.projectSecurityDepositRepository = projectSecurityDepositRepository;
        }

        public ProjectSecurityDeposit? GetProjectSecurityDeposit(int id)
        {
            return projectSecurityDepositRepository.Get(id);
        }

        public void Create(ProjectSecurityDeposit entity)
        {
            projectSecurityDepositRepository.Insert(entity);
        }

        public void Update(ProjectSecurityDeposit entity)
        {
            projectSecurityDepositRepository.Update(entity);
        }

        public IPagedList<ProjectSecurityDeposit> GetProjectSecurityDeposits(ProjectSecurityDepositQuery query, int pageIndex, int pageSize)
        {
            return projectSecurityDepositRepository.GetProjectSecurityDeposits(query, pageIndex, pageSize);
        }

        public void Delete(int id)
        {
            projectSecurityDepositRepository.DeleteByEntityId(id);
        }

        public void Deleted(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                Delete(id);
            }
        }

        /// <summary>
        /// 生成编号和流水号
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public (string ProjectNum, int SerialNumber) GetNumAndSerialNumber(string projectNum, int projectId)
        {
            DateTime time = DateTime.Now;
            projectNum = projectNum.Replace(SerialNumberType.项目.GetDescription(), SerialNumberType.保证金.GetDescription());
            ProjectSecurityDeposit? data = projectSecurityDepositRepository.GetProjectSecurityDepositByMaxSerialNumber(time, projectId);
            int SerialNumber = 1;
            if (data != null)
                SerialNumber = data.SerialNumber + 1;
            string ProjectNum = $"{projectNum}-{SerialNumber.ToString().PadLeft(3, '0')}";
            return (ProjectNum, SerialNumber);
        }

        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public string GetCode(string projectNum, int projectId)
        {
            projectNum = projectNum.Replace(SerialNumberType.项目.GetDescription(), SerialNumberType.专户.GetDescription());
            int serialNumber = GetProjectSecurityDepositByMaxSerialNumber(projectId);
            return $"{projectNum}-{serialNumber.ToString().PadLeft(3, '0')}";
        }

        /// <summary>
        /// 生成流水号
        /// </summary>
        /// <returns></returns>
        public int GetProjectSecurityDepositByMaxSerialNumber(int projectId)
        {
            DateTime time = DateTime.Now;
            ProjectSecurityDeposit? data = projectSecurityDepositRepository.GetProjectSecurityDepositByMaxSerialNumber(time, projectId);
            if (data == null)
                return 1;
            else return data.SerialNumber + 1;
        }
    }
}